package com.situ.yB2C.user.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.situ.yB2C.address.Service.AddressService;
import com.situ.yB2C.address.pojo.Address;
import com.situ.yB2C.area.pojo.Area;
import com.situ.yB2C.cart.pojo.Cart;
import com.situ.yB2C.cart.service.CartService;
import com.situ.yB2C.catalog.service.CatalogService;
import com.situ.yB2C.product.service.ProductService;
import com.situ.yB2C.user.pojo.User;
import com.situ.yB2C.user.service.UserService;
import com.situ.yB2C.util.ContextUtil;

@Controller
public class UserController {
	private static final Logger LOG = LoggerFactory.getLogger(UserController.class);

	@Autowired
	private UserService userService;

	@Autowired
	private CatalogService catalogService;
	@Autowired
	private ProductService productService;
	@Autowired
	private CartService cartService;
	@Autowired
	private AddressService addressService;
	

	// 进入订单页面
	@RequestMapping("order_list")
	public ModelAndView goOrderList(ModelAndView modelAndView) {

		modelAndView.setViewName("shop/order_list");
		return modelAndView;
	}

	// 提交后的系统提示订单页面
	@RequestMapping("system_prompts")
	public ModelAndView goSystemPrompts(ModelAndView modelAndView) {

		modelAndView.setViewName("shop/system_prompts");
		return modelAndView;
	}

	// 进入确认订单
	@RequestMapping("order_confirm")
	public ModelAndView goOrderConfirm(HttpSession session, ModelAndView modelAndView) {
		Object object = session.getAttribute(ContextUtil.SESSION_CART_NAME);
		if (object != null) {
			modelAndView.addObject("cartList", (List<Cart>) object);
		}
		modelAndView.setViewName("shop/order_confirm");

		return modelAndView;
	}

	/**
	 * # 商品展示
	 * 
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping("channel")
	public ModelAndView goShop(ModelAndView modelAndView) {
		// 查询所有目录
		modelAndView.addObject("cataList", catalogService.findTreeCatalogList());
		// 查询所有的商品信息
		modelAndView.addObject("productList", productService.findAllProduct());
		modelAndView.setViewName("shop/channel");
		return modelAndView;
	}

	// 进入商品详情
	@RequestMapping("product")
	public ModelAndView goProduct(ModelAndView modelAndView) {
		
		/* modelAndView.addObject("productList", productService.findAllProduct()); */
		modelAndView.setViewName("shop/product");
		return modelAndView;
	}

	// 进入购物车
	@RequestMapping("cart")
	public ModelAndView goCat(HttpSession session, ModelAndView modelAndView) {
		Object object = session.getAttribute(ContextUtil.SESSION_CART_NAME);
		if (object != null) {
			modelAndView.addObject("cartList", (List<Cart>) object);
		}
		modelAndView.setViewName("shop/cart");

		return modelAndView;
	}
//	// 进入收货地址
//		@RequestMapping("shop/doaddress")
//		public ModelAndView doAddress(Address address, ModelAndView modelAndView) {
//			//modelAndView.addObject("userList", userService.findAllUser());
//			//addressService.s
//			addressService.saveAddress(address);
//			modelAndView.addObject("addressList", addressService.findAddressList());
//			
//			modelAndView.setViewName("redirect:/shop/address");
//			return modelAndView;
//		}
	
	// 进入收货地址
	@ResponseBody
	@RequestMapping("shop/address")
	public ModelAndView goAddress(Address address, ModelAndView modelAndView) {
		
		modelAndView.addObject("addressList", addressService.findAddressList());
		
		modelAndView.setViewName("shop/address");
		return modelAndView;
	}

	// 进入个人中心
	@RequestMapping("shop/users")
	public ModelAndView goUsers(ModelAndView modelAndView) {
		modelAndView.setViewName("users");
		return modelAndView;
	}

	@ResponseBody
	@RequestMapping("list")
	public List<User> findAllRoleList() {
		return userService.findAllUser();
	}

	// 执行注册
	@RequestMapping("shop/doregister")
	public ModelAndView doRegister(User user, ModelAndView modelAndView) {
		// LOG.debug(user.toString());
		// return userService.saveUser(user);
		userService.saveUser1(user);
		user.setUserKind(User.USER_KIND_BUYER);
		modelAndView.addObject("userList", userService.findAllUser());
		modelAndView.setViewName("redirect:/shop/gologin");
		return modelAndView;
	}

	// 进入注册
	@RequestMapping("shop/goregister")
	public ModelAndView goRegister(ModelAndView modelAndView) {
		modelAndView.setViewName("register");
		return modelAndView;
	}

	@RequestMapping("checkCode")
	public int checkUserCode(String userCode) {
		return userService.checkUserCode(userCode);
	}

	// 退出登录
	@ResponseBody
	@RequestMapping("/doOutLogin")
	public ModelAndView doOutLogin(ModelAndView modelAndView, HttpSession session, HttpServletRequest request) {
		session = request.getSession();
		session.removeAttribute(ContextUtil.SESSION_LOGIN_BUYER);
		modelAndView.setViewName("index");
		return modelAndView;
	}

	// 进登录
	@RequestMapping("shop/gologin")
	public ModelAndView gologin(ModelAndView modelAndView) {
		modelAndView.setViewName("login");
		return modelAndView;
	}

	// 登录
//	@RequestMapping("shop/dologin")
//	public ModelAndView goLoginPage(User user, ModelAndView modelAndView, HttpServletRequest request) {
//		HttpSession session = request.getSession();
//		LOG.debug(user.toString());
//		modelAndView.addObject("cataList", catalogService.findTreeCatalogList());
//		modelAndView.addObject("productList", productService.findAllProduct());
//		int result = userService.doUserLogin(user.getUserCode(), user.getUserPass(), session);
//		if (result == 1) {
//			modelAndView.setViewName("index");
//		} else {
//			
//			modelAndView.setViewName("redirect:/shop/gologin");
//		}
//		return modelAndView;
//	}
//	@ResponseBody
//	@RequestMapping("shop/dologin")
//	public int doUserLogin(String userCode,String userPass,Integer remember, HttpServletRequest request ,HttpServletResponse response) {
//		int result = userService.doBuyerLogin(userCode,userPass,remember,request,response);
//		System.out.println("+++++++++++买家登陆结果+++++++++++++"+result);
//		
//		return result;
//	}
	// 执行登录
	@ResponseBody
	@RequestMapping("shop/dologin")
	public int doUserLogin(String userCode, String userPass, Integer remember, HttpServletRequest request,
			HttpServletResponse response) {
		return userService.doBuyerLogin(userCode, userPass, remember, request, response);
	}

	// 进入首页
	@RequestMapping({ "/", "/index" })
	public ModelAndView gonIndex(ModelAndView modelAndView) {
		modelAndView.addObject("userList", userService.findAllUser());
		modelAndView.addObject("cataList", catalogService.findTreeCatalogList());
		modelAndView.addObject("productList", productService.findAllProduct());
		modelAndView.setViewName("index");
		return modelAndView;
	}

	// 进入商家后台首页1
	@RequestMapping("user/goindex2")
	public ModelAndView goindex2User(ModelAndView modelAndView) {
		modelAndView.setViewName("user/user_index");
		return modelAndView;
	}

	// 进入商家后台首页
	@RequestMapping("user/index2")
	public ModelAndView goIndex2(ModelAndView modelAndView) {
		modelAndView.addObject("userList", userService.findAllUser());
		modelAndView.setViewName("index2");
		return modelAndView;
	}

	// 进入商家的登录
	@RequestMapping("user/gologin2")
	public ModelAndView goLoginPage(ModelAndView modelAndView) {

		modelAndView.setViewName("login2");
		return modelAndView;
	}

	// 执行登录
	@ResponseBody
	@RequestMapping("user/dologin2")
	public int doLoginByAjax(String userCode, String userPass, HttpSession session) {
		// System.out.println(userCode);
		int result = userService.doUserLogin(userCode, userPass, session);
		return result;
	}

	// 进入商家后台 增加页面
	@RequestMapping("user/goadd")
	public ModelAndView goAddUser(ModelAndView modelAndView) {
		modelAndView.setViewName("user/user_add");
		return modelAndView;
	}

//	// 进入商家后台 执行页面
//	@RequestMapping("user/doadd")
//	public ModelAndView doAddUser(User user,MultipartFile userFile ,HttpServletRequest requeset,
//			 ModelAndView modelAndView) {
//		LOG.debug(userFile.toString());
//		LOG.debug(user.toString());
//		//TONGGUO
//		ServletContext application = requeset.getServletContext();
//		String realPath = application.getRealPath("/");
//		LOG.debug("realPath="+realPath);
//		//保存数据
//		userService.saveUser(user,userFile,realPath);
//		//保存完成后，应该但回首页
//		//modelAndView.addObject("userList",userService.findAllUser());
//		//modelAndView.setViewName("user/user_index");
//		
//		//做出重定向 redirect  forward 
//		modelAndView.setViewName("redirect:/user/user_index");
//		return modelAndView;
//	}
	//
	@ResponseBody
	@RequestMapping("user/list")
	public List<User> findAllUserList() {
		return userService.findAllUser();
	}

	// 执行新增
	@ResponseBody
	@RequestMapping("user/doadd")
	public int doAddRole(User user) {
		LOG.debug(user.toString());

		return userService.saveUser1(user);

	}

	// 执行
	@ResponseBody
	@RequestMapping("user/doeidt")
	public int doEdit(User user) {
		return userService.updateUser(user);
	}

	// 进入修改
	@ResponseBody
	@RequestMapping("user/goedit")
	public ModelAndView goEditRole(Long rowId, ModelAndView modelAndView) {
		User user = userService.findUserById(rowId);
		modelAndView.addObject("user", user);
		modelAndView.setViewName("user/user_edit");
		return modelAndView;
	}

	// 执行删除
	@ResponseBody
	@RequestMapping("user/dodelete")
	public int doDeleteRole(Long rowId) {
		return userService.doDeleteUserById(rowId);
	}

}
